var Order = Vue.component('order', function (resolve, reject) {
    axios.get('views/order/order.html?timestamp=' + timestamp).then(function (res) {
        resolve({
            template: res.data,
            data() {
                return {
                    tableData: [],
                    dialogFormVisible: false,
                    form: {},
                    searchForm: {},
                    searchOptions: {
                        lifeManagerUser: [],
                        salesUser: [],
                        life: [],
                        orderStatusSearchOptions: [],
                        product: []
                    },
                    pageInfo: {
                        currentPage: 1,
                        pageSize: 10,
                        srch_EQ_delete: false,
                        totalPage:1
                    },
                    formLabelWidth: '120px',
                    permssion: {},
                    buttomPermission: {
                        searchSales: [admin, alipayManager,access],
                        searchLifeManager: [admin,access],
                        lifeManagerAndSaler: [alipayManager,sales]
                    },
                    showSteps: false,
                    orderStatusJson:{},
                    detailData:{},
                    gridData :{},
                    detailDialog:false,
                    fileAction:APIs.order.upload,
                    uploadHead:{token:sessionStorage.token},
                    uploadFileName:"",
                    dialogVisible:false,
                    dialogVisibleMsg:"",
                    sentDialogFormVisible:false,
                    cancelDialogFormVisible:false,
                    sentForm:{},
                    YesOrType:{"1":"是","0":"否"},
                    stepList:[],
                    newOrderStatusLog:{},
                    checkCountInterval:null

                   
                }
            },
            methods: {
                onSubmit: function () {
                    console.log(this.searchForm);
                    // this.pageInfo.currentPage=1;
                    this.$set(this.pageInfo,'currentPage',1);
                    this.getOrderList();
                },
                checkPermission: function (type) {
                    let checkArr = this.buttomPermission[type];
                    if (checkArr) {
                        for (var v in checkArr) {
                            if (this.permssion[checkArr[v]]) {
                                return true;
                            }
                        }
                    } else {
                        return true;
                    }
                    return false;
                },
                getPageConfig: function () {
                    httpInvoker.get(APIs.order.config)
                        .then(res => {
                            if (res.status == 200 && res.data.status == '00') {
                                var data = res['data'];
                                if (data.status == '00') {
                                    this.$set(this.searchOptions, 'lifeManagerUser', (data['data'].lifeManagerUser ? data['data'].lifeManagerUser : []));
                                    this.$set(this.searchOptions, 'salesUser', (data['data'].salesUser ? data['data'].salesUser : []));
                                    this.$set(this.searchOptions, 'life', (data['data'].life ? data['data'].life : []));
                                    this.$set(this.searchOptions, 'orderStatusSearchOptions', (data['data'].orderStatusSearchOptions ? data['data'].orderStatusSearchOptions : []));
                                    if(this.searchOptions.orderStatusSearchOptions){
                                        this.searchOptions.orderStatusSearchOptions.forEach(element => {
                                            this.$set(this.orderStatusJson, element['value'], element['label']);

                                        });
                                    }

                                }
                                this.getOrderList();
                            } else {
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                           
                        })
                  
                },
                changeSelect: function (type) {
                    if ('lifeManager' == type) {
                        this.$set(this.searchForm, 'life', null)
                    }
                    this.$set(this.searchForm, 'product', null)
                    this.$set(this.searchForm, 'saler', null)
                    httpInvoker.post(APIs.order.config + "/" + type, this.searchForm)
                        .then(res => {
                            this.logining = false;
                            if (res.status == 200 && res.data.status == '00') {
                                var map = res['data'];
                                if (map.status == '00') {
                                    var data = map['data'];
                                    var salesUser = data['salesUser'];
                                    var life = data['life'];
                                    var product = data['productList'];
                                    if ('lifeManager' == type) {
                                        this.$set(this.searchOptions, 'life', (life ? life : []))
                                    } else if ('life' == type) {

                                    }
                                    this.$set(this.searchOptions, 'product', (product ? product : []))
                                    this.$set(this.searchOptions, 'salesUser', (salesUser ? salesUser : []))
                                }

                            } else {
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        })
                }, getOrderList: function () {
                    httpInvoker.post(APIs.order.list + "/" + this.pageInfo.currentPage, this.searchForm)
                        .then(res => {
                            if (res.status == 200 && res.data.status == '00') {
                                this.tableData = res.data.data.content;
                                this.pageInfo.totalPage = res.data.data.totalElements;
                                this.initInterval();
                            } else {
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        });
                },
                openSteps: function (orderNo) {
                    this.showSteps = true;
                    var url = APIs.order.orderStatusList+'/'+orderNo;
                    httpInvoker.post(url)
                    .then(res => {
                        if (res.status == 200 && res.data.status == '00') {
                            console.log(res.data.data);
                            // this.$set("stepList",)
                            this.stepList = res.data.data;

                        } else {
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    });
                }, handleCurrentChange: function (currentPage) {
                    this.pageInfo.currentPage   = currentPage;
                    this.getOrderList();
                },openDetail:function(id,openType){
                    var url = APIs.order.getOrderDetail+'/'+id;
                    httpInvoker.post(url)
                    .then(res => {
                        this.detailDialog  = true;
                        if (res.status == 200 && res.data.status == '00') {
                            this.detailData = res.data.data.detail;
                            this.newOrderStatusLog = res.data.data.statusLog;
                            this.detailData.isSend = this.detailData['deliveryTime']?"1":"0";
                            openType = openType?openType:'detail';
                            this.detailData.openType = openType;
                            console.log( this.detailData);
                        } else {
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    });
                },download(){
                    window.open(APIs.order.download+"?downLoadId="+sessionStorage.token, '_blank');
                },beforeUpload(file){
                    var reg = new RegExp(".csv");
                    if(reg.test(file.name)){
                        this.uploadFileName = file.name;
                        this.$store.commit('showLoading');
                        return true;
                    }else{
                        this.dialogVisible=true;
                        this.dialogVisibleMsg = "文件格式不正确!";
                        this.uploadFileName = "";
                    }
                    
                    return false;
                },successUpload(response, file, fileList){
                    console.log(response);
                    this.$store.commit('hideLoading');
                    var status =response["status"];
                    var msg = response['msg'];
                    if(response&&status&&"00"==status){
                        this.dialogVisible=true;
                        this.dialogVisibleMsg = "上传成功";
                        this.getOrderList();
                    }else{
                        this.dialogVisible=true;
                        this.dialogVisibleMsg = msg?msg:"上传失败";
                    }
                    
                },removeFile(){
                    this.$refs.upload.clearFiles();
                    this.uploadFileName="";
                },openSent(row){
                    var url = APIs.order.getOrderDetail+'/'+row.id;
                    httpInvoker.post(url)
                    .then(res => {
                        this.sentDialogFormVisible  = true;
                        if (res.status == 200 && res.data.status == '00') {
                            this.$set(this.sentForm, 'orderNo', row.orderNo);
                            this.$set(this.sentForm, 'productName', row.productName);
                            this.$set(this.sentForm, 'location', res.data.data.detail['location']);
                            this.$set(this.sentForm, 'applyUserName', res.data.data.detail['applyUserName']);
                            this.$set(this.sentForm, 'applyTel',  res.data.data.detail['applyTel']);
                            this.$set(this.sentForm, 'id',  row.id);


                        } else {
                            this.$alert(res.data.msg, '', {
                                confirmButtonText: '确定'
                            });
                        }
                    });
                },sent(){
                   
                    
                    var validFlag = false;
                    this.$refs['sentForm'].validate((valid) => {
                        validFlag = valid;
                    });
                    if(validFlag){
                        var url = APIs.order.sent;
                        httpInvoker.post(url+'/'+this.sentForm['id'],this.sentForm)
                        .then(res => {
                            if (res.status == 200 && res.data.status == '00') {
                                this.sentDialogFormVisible  = false;
                                this.dialogVisible=true;
                                this.dialogVisibleMsg = "发货成功!";
                                this.getOrderList();
                            } else {
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        });
                    }
                },issue(type,obj){

                    var options = {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'info'
                    };
                    if(type!='1'&&type != '0'){
                        return;
                    }
                    var title ="1"==type?"激活订单:"+obj['orderNo']:"订单:"+obj['orderNo']+"过期未激活收取押金";
                    // console.log(type,obj);
                    this.$confirm(title, options).then(()=>{
                        console.log(1111111111);
                        var params = {"type":type};
                        httpInvoker.post(APIs.order.issue+"/"+obj.id,params)
                        .then(res => {
                            if (res.status == 200 && res.data.status == '00') {
                                // this.sentDialogFormVisible  = false;
                                this.dialogVisible=true;
                                this.dialogVisibleMsg = "更新成功!";
                                this.getOrderList();
                            } else {
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        });
                    }).catch(()=>{

                    });
                    
                },openCancel(row){
                   this.openDetail(row.id,'cancel');
                   if(this.$refs['detailDataForm']&&this.$refs['detailDataForm'].clearValidate){
                       this.$refs['detailDataForm'].clearValidate();
                    }
                },submitCance(){
                    var validFlag = false;
                    this.$refs['detailDataForm'].validate((valid) => {
                        validFlag = valid;
                    });
                    if(!validFlag)return;
                    var options = {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'info'
                    };
                    var title = "确定退款?(订单号:"+this.detailData['orderNo']+")";
                    this.$confirm(title, options).then(()=>{
                        var params = {"remark":this.detailData['remark']};
                        httpInvoker.post(APIs.order.cancel+"/"+this.detailData.id,params)
                        .then(res => {
                            if (res.status == 200 && res.data.status == '00') {
                                // this.sentDialogFormVisible  = false;
                                this.detailDialog=false;
                                 this.dialogVisible=true;
                                this.dialogVisibleMsg = "退款成功!";
                                this.getOrderList();
                            } else {
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        });
                    }).catch(()=>{

                    });
                },openDelete(row){
                    var options = {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    };
                    var title = "确定作废订单?(订单号:"+row['orderNo']+",订单作废后无法恢复)";
                    this.$confirm(title, options).then(()=>{
                        httpInvoker.post(APIs.order.delete+'/'+row.id,{})
                        .then(res => {
                            if (res.status == 200 && res.data.status == '00') {
                                // this.sentDialogFormVisible  = false;
                                this.detailDialog=false;
                                 this.dialogVisible=true;
                                this.dialogVisibleMsg = "作废成功!";
                                this.getOrderList();
                            } else {
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        });
                    }).catch(()=>{

                    });
                 },batchIssue(){
                    var options = {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'info'
                    };
                    
                    var title ="确定批量处理过期未激活订单收取押金?";
                    // console.log(type,obj);
                    this.$confirm(title, options).then(()=>{
                        httpInvoker.post(APIs.order.issue,{})
                        .then(res => {
                            if (res.status == 200 && res.data.status == '00') {
                                // this.sentDialogFormVisible  = false;
                                this.dialogVisible=true;
                                this.dialogVisibleMsg = "批量处理完成!";
                                this.getOrderList();
                            } else {
                                this.$alert(res.data.msg, '', {
                                    confirmButtonText: '确定'
                                });
                            }
                        });
                    }).catch(()=>{

                    });
                 },checkHasNewOrderReord(type){
                    var url = APIs.order.checkHasNewOrderReord+'/'+type;
                    httpInvoker.post(url,{})
                    .then(res => {
                      
                        if (res.status == 200 && res.data.status == '00'&&res.data.data) {
                            // this.sentDialogFormVisible  = false;
                            this.$store.commit('setTitle', {
                                name: '(有新的订单)'
                              });
                             this.clearChckInterval();
                        } else {

                            this.$store.commit('setTitle', {
                                name: ''
                              });
                        }
                    });
                 },initInterval(){
                    if(this.checkPermission('lifeManagerAndSaler')){
                        if(!this.checkCountInterval){
                            console.log('####init interval');
                            this.checkHasNewOrderReord(0);
                            this.checkCountInterval= setInterval(()=>{
                                this.checkHasNewOrderReord(1);
                            },1000*60*1)
                        }
                    }
                 },clearChckInterval(){
                     if(this.checkCountInterval){
                        console.log('####clear interval');
                        clearInterval(this.checkCountInterval);
                        this.checkCountInterval = null;
                     }
                 }
                 
            },
            created: function () {
                var json = CommonUtils.getPermissionJson();
                this.permssion = json;
                this.getPageConfig();
               
            },destroyed:function(){
                this.$store.commit('setTitle', {
                    name: ''
                  })
                  clearInterval(this.checkCountInterval);
            }
            
        })
    })
})